<?php
  defined('_JEXEC') or die();
  jimport('joomla.application.component.model');
  require_once('base.php');
  
  class VideoModelVideo extends BaseModel
  {     
    var $_name        = "video";
    var $_table       = "#__pbny_videos";
    
    function save($data){
      
      //sanitize video
      $str = $data['video'];
      if(strpos($data['video'], '&') !== false)
        $data['video'] = substr($str, 0, strpos($str,'&'));
      
      parent::save($data);
    }
    
    function get_video_embed($video){
	
	  # youtube
	  if(preg_match('/youtube.com/', $video)){
        $video_id = substr($video, strpos($video,'=') + 1);
        $result   = "http://www.youtube.com/embed/";
        $result  .= $video_id;
      
        if(strlen($result) != 40)
          return null;
        return $result;
	  } #vimeo
	  elseif(preg_match('/vimeo.com/', $video))
	    return $video;
	  
      return null;
    }
    
    function get_video_thumbnail($video){
	
	  # youtube
	  if(preg_match('/youtube.com/', $video)){
        $video_id = substr($video, strpos($video,'=') + 1);
        $result   = "http://img.youtube.com/vi/$video_id/0.jpg";
        return $result;
	  }
	  elseif(preg_match('/vimeo.com/', $video)){
        $video_id = substr($video, strrpos($video,'/') + 1);
		$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video_id.php"));
		if($hash[0])
		  return $hash[0]['thumbnail_small'];
	  }
	  return null;
    }
  
    function get_video_types(){
      
      $query = "SHOW COLUMNS FROM ".$this->_table." WHERE field = 'video_type'";
      $db = $this->getDBO();
      $db->setQuery($query);
      $row = $db->loadRow();
      
      $result = str_replace(array("enum(",")","'"),"",$row[1]);
      return explode(",",$result);
    }
    
    function get_video_type_instance($video_type, $id){
      
      $result = null;
      
      switch($video_type){
        case "SEASONS":
          $season   = $this->getInstance('season', 'seasonmodel');
          $s      = $season->get($id);
          
          if($s)
            $result = $s->name;
        break;
        case "TEAMS":
          $team     = $this->getInstance('team', 'teammodel');
          $t        = $team->get($id);
          
          if($t)
            $result = $t->name;
          
        break;
        case "PLAYERS":
          $player   = $this->getInstance('player', 'playermodel');
          $person   = $this->getInstance('person', 'personmodel');
          
          $p      = $player->get($id);
          if($p)
            $result = $person->get_fullname($p->person_id);
          
        break;
        case "VENUES":
          $venue   = $this->getInstance('venue', 'venuemodel');
          $v     = $venue->get($id);
          
          if($v)
            $result = $v->name;
          
        break;
        case "GAMES":
          $team       = $this->getInstance('team', 'teammodel');
          $schedule   = $this->getInstance('schedule', 'schedulemodel');
          $s      = $schedule->get($id);
          
          if($s){
            $t = $team->get($s->first_team_id);
            $t2 = $team->get($s->second_team_id);
            $text = "$t->name VS. $t2->name ".strftime('%Y-%m-%d %H:%M',strtotime($s->schedule));
            $result = $text;
          }
        break;
      }
      
      return $result;
    }
    
    function get_video_type_instances($video_type){
      
      $data = array();
      
      switch($video_type){
        case "SEASONS":
          $season   = $this->getInstance('season', 'seasonmodel');
          $seasons  = $season->get();
          
          if($seasons){
            foreach($seasons as $s)
              $data[$s->id] = $s->name;
          }
          
        break;
        case "TEAMS":
          $team   = $this->getInstance('team', 'teammodel');
          $teams  = $team->get();
          
          if($teams){
            foreach($teams as $t)
              $data[$t->id] = $t->name;
          }
          
        break;
        case "PLAYERS":
          $player   = $this->getInstance('player', 'playermodel');
          $person   = $this->getInstance('person', 'personmodel');
          
          $players  = $player->get();
          if($players){
            foreach($players as $p)
              $data[$p->id] = $person->get_fullname($p->person_id);
          }
        break;
        case "VENUES":
          $venue   = $this->getInstance('venue', 'venuemodel');
          $venues  = $venue->get();
          
          if($venues){
            foreach($venues as $v)
              $data[$v->id] = $v->name;
          }
        break;
        case "GAMES":
          $team       = $this->getInstance('team', 'teammodel');
          $schedule   = $this->getInstance('schedule', 'schedulemodel');
          $schedules  = $schedule->get();
          
          if($schedules){
            foreach($schedules as $s){
              $t = $team->get($s->first_team_id);
              $t2 = $team->get($s->second_team_id);
              $text = "$t->name VS. $t2->name ".strftime('%Y-%m-%d %H:%M',strtotime($s->schedule));
              $data[$s->id] = $text;
            }
          }
        break;
      }
      return $data;
    }
    
    function get_video_gallery(){
      
      $sql = 'SELECT * FROM '.$this->_table.
             " WHERE video_type in ('TEAMS','VENUES','SEASONS','GAMES')";
      return $this->_get($sql);
    }
  }
?>